package com.hmall.search.mq;

import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author www
 * @Description 商品监听器
 * @Date 2023/4/11 12:26
 * @Version 1.0
 */
@Component
public class ItemListener {
    /**
     * 注入 ISearchService
     */
    @Resource
    private ISearchService searchService;

    /**
     * 绑定上架状态
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "up.queue"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "item.up"
    ))
    public void listenItemUp(Long id){
        searchService.saveItemById(id);
    }

    /**
     * 绑定下架状态
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "down.queue"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "item.down"
    ))

    /**
     * 为下架状态时根据id删除 商城商品
     */
    public void listenItemDown(Long id){
        searchService.deleteItemById(id);
    }
}
